package com.example.demo.leetcode.classics150;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _108将有序数组转换为二叉搜索树
 * @description
 * @date 2023/08/12 21:52
 * <p>
 * ******************************************************
 */
public class _108将有序数组转换为二叉搜索树 {

    public TreeNode sortedArrayToBST(int[] nums) {
        return transfer(nums, 0, nums.length - 1);
    }

    private TreeNode transfer(int[] nums, int i, int j) {
        if (i > j) {
            return null;
        }
        int index = i + (j - i) / 2;
        TreeNode root = new TreeNode(nums[index]);
        root.left = transfer(nums, i, index - 1);
        root.right = transfer(nums, index + 1, j);
        return root;
    }




    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}

